package es.cazadoresmadridejos.objs.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import es.cazadoresmadridejos.model.Partner;
import es.cazadoresmadridejos.model.Partner.Role;
import es.cazadoresmadridejos.model.Reason;

public class User implements UserDetails{

	private static final String salt ="D3JkldsMMsx13";
	
	public static String getSalt() {
		return salt;
	}

	private Partner partner;

	public Partner getPartner() {
		return partner;
	}

	public void setPartner(Partner partner) {
		this.partner = partner;
	}

	public User(Partner partner){
		this.partner = partner;
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
		GrantedAuthority authority = new GrantedAuthority() {
			@Override
			public String getAuthority() {
				return partner.getRole().name();
			}
		};
		list.add(authority);
		//Un miembro de la junta también es cazador
		if(partner.getRole().equals(Role.JUNTA)){
			GrantedAuthority authorityHunter = new GrantedAuthority() {
				@Override
				public String getAuthority() {
					return Role.CAZADOR.name();
				}
			};
			
			list.add(authorityHunter);
		}
		
		
		return list;
	}

	@Override
	public String getPassword() {
		return partner.getPassword();
	}

	@Override
	public String getUsername() {
		return partner.getUserid();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return partner.getActive() == 1;
	}

	public Reason getReason(){
		return partner.getSelectedReason();
	}
}
